package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.pojo.Car;
import com.chixing.pojo.CarExample;
import com.chixing.service.CarService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {


    @Autowired
    private CarMapper carMapper;

    @Override
    public ServerResponse getCarDetail(Integer carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        if (car != null)
            return ServerResponse.getSucccess(carId);
        return ServerResponse.getFail(null);
    }

    @Override
    public Car getCarById(Integer carId) {
        return carMapper.selectByPrimaryKey(carId);
    }

    @Override
    public ServerResponse FindByConditions(String carCategoryName, String carBrandname, Integer pageNum) {
        CarExample carExample1 = new CarExample();
        CarExample.Criteria carCriteria = carExample1.createCriteria();

        if(carCategoryName.equals("全部"))
            carCategoryName = null;
        if(carBrandname.equals("全部"))
            carBrandname = null;
        if(carCategoryName != null){
            carCriteria.andCarCategoryNameEqualTo(carCategoryName);
        }
        if(carBrandname != null){
            carCriteria.andCarBrandnameEqualTo(carBrandname);
        }
        PageHelper.startPage(pageNum,8);
        List<Car> carList1 = carMapper.selectByExample(carExample1);
        PageInfo<Car> pageInfo = new PageInfo<>(carList1);

        if(carList1 != null && carList1.size() > 0){
            return ServerResponse.getSucccess(pageInfo);
        }else {
            return ServerResponse.getFail(null);
        }
    }

    @Override
    public ServerResponse getByPage(Integer pageNum) {
        PageHelper.startPage(pageNum,8);
        List<Car> dishList = carMapper.selectByExample(null);
        PageInfo<Car> pageInfo =  new PageInfo<>(dishList); // pageInfo(dish 数据 + 页码)
        return ServerResponse.getSucccess(pageInfo);
    }

}
